/**
  * Scala编程实战 map,flatten和flatMap的组合
  * P283
  */

object collection1016 extends App {

  //扁平化
  val complesA = List("two", "three", "four", "five")

  val complesB = List(List("two", "three"), List("four", "five"))

  val bag = List("1", "2", "three", "4", "one hundred sevent five")

  def toInt(str: String): Option[Int] = {
    try {
      Some(Integer.parseInt(str))
    } catch {
      case e: Exception => None
    }
  }

  println(complesA.flatten)
  println(complesB.flatten)

  println(bag.flatten(toInt))

  println(bag.map(toInt).flatten)

  println(bag.flatMap(toInt))
}
